#	Plugin interface
#		methods: search, search_by_ref, init, title, version, desc
#
#
#
#	Grabber plugin
#		TTVDB
#		Uses the ttvdb.py script that comes with Mythtv 0.22
#		Requires python 2.5 and elementtree module

package grabbers::ttvdb_old;

use HTML::Parser;
use HTML::TreeBuilder;
use HTML::Entities;
use LWP::Simple;
use LWP::UserAgent;
use Getopt::Long;
use Text::Levenshtein qw(distance);


# Exports
our $VERSION = "1.1";
sub title
{
	return "Ttvdb";
}
sub desc
{
	return "Search on The TV database using Mythtv ttvdb.py";
}
sub version
{
	my $ver_string;
	$ttvdb_pid = open(RESULT, "$ttvdb_script -v |");
	unless ($ttvdb_pid)
	{
		die "Can't run $ttvdb_script";
	}
	while ( $line = <RESULT>)
	{
		if($line =~ /Version: \((.+?)\)/)
		{
			$ver_string = $1;
			last;
		}
		elsif($line =~ /\((.+?)\)/)
		{
			$ver_string = $1;
			last;
		}
	}
	close(RESULT);
	return $VERSION.", ttvdb.py: ".$ver_string;
}
#end Exports

our $ttvdb_script = "";
our $broken = 0;

sub search 
{
	my $self = shift;
	my $title = shift;
	my $subtitle = shift;
	my $season = shift;
	my $episode = shift;
	my $year = shift;
	my %details;
	if ( $broken == 0 )
	{
		if( (length $season == 0 || $season == 0) && (length $episode == 0 || $episode == 0) )
		{
			print " Not a TV show\n";
		}
		else
		{
			if( length $season == 0)
			{
				# if no season info, then assume it is season 1
				$season = 1;
			}
			$result = getTtvdbData(\%details, $title, $subtitle, $season, $episode, $year);
		}
	}
	else
	{
		print " Disabled\n"
	}
	return %details;
}

sub search_by_ref
{
	my $self = shift;
	my $ref = shift;
	my $details;	
	init();
	if(length $ref > 0)
	{
		getImdbData(\$details, $ref);
		getImdbEpisodeData(\$details, $ref);
	}
	return $details;
}

sub init
{
	my $nReturn = 1;
	#look for script
	if( -x "/usr/share/mythtv/mythvideo/scripts/ttvdb.py")
	{
		$ttvdb_script = "/usr/share/mythtv/mythvideo/scripts/ttvdb.py";
	}
	elsif( -x "/usr/share/mythtv/mythvideo/scripts/Television/ttvdb.py")
	{
		$ttvdb_script = "/usr/share/mythtv/mythvideo/scripts/Television/ttvdb.py";
	}
	elsif( -x "/usr/local/share/mythtv/mythvideo/scripts/ttvdb.py")
	{
		$ttvdb_script = "/usr/local/share/mythtv/mythvideo/scripts/ttvdb.py";
	}
	else
	{
		print "ttvdb_old.pm: Can't find script \"ttvdb.py\"\n";
		$nReturn = 0;
	}
	return $nReturn;
}

sub new
{
    my($class, %args) = @_;
    my $self = bless({}, $class);
    
    if(init())
	{
		return $self;
	}
    else
	{
	    return 0;
	}
}
#######################################
##   UTILITIES
#######################################
sub getTtvdbData
{
	my $file_details = shift;
	my $title = shift;
	my $subtitle = shift;
	my $season = shift;
	my $episode = shift;
	my $year = shift;
	my $gotimages = 0;

	$ttvdb_pid = open(RESULT, "$ttvdb_script -M \"$title\" $season $episode 2>&1 |");
	unless ($ttvdb_pid)
	{
		die "Can't run $ttvdb_script";
	}
	#we may get multiple hits, find best match
	my $line;
	my $line_ref = "";
	my $line_title = "";
	my $best_score = 1000;
	my $best_title = "";
	my $errors;
	while ( $line = <RESULT>)
	{
		if($line =~ /Error contacting www\.thetvdb\.com/)
		{
			$broken = 1;
			print " Site appears to be down, disabling grabber\n";
		}
		($line_ref,$line_title) = $line =~ m/(\d+):(.+)/;
		($line_title_part,$line_year) = $line_title =~ m/(.+?) ?(\(\d{0,4}\))?$/;
		$line_year =~ s/[\(\)]//g;
		if($line_year < 1900)
		{
			undef $line_year;
		}
		$errors = distance(lc($title), lc($line_title_part));
		if($errors < $best_score)
		{
			$best_score = $errors;
			$best_title = $line_title;
			$file_details->{Title} = $line_title_part;
		}
		elsif($errors == $best_score)
		{
			if($year == $line_year)
			{
				$best_score = $errors;
				$best_title = $line_title;
				$file_details->{Title} = $line_title_part;
			}
		}
	}
	close(RESULT);
	if( $broken )
	{
		return;
	}
	$ttvdb_pid = open(RESULT, "$ttvdb_script -D \"$best_title\" $season $episode 2>&1 |");
	unless ($ttvdb_pid)
	{
		die "Can't run $ttvdb_script";
	}
	while ( $line = <RESULT>)
	{
#		if($line =~ /Title:(.+)/)
#		{
#			$file_details->{Title} = $1;
#			$file_details->{Title} =~ s/\"//g;
#		}
		if($line =~ /Season:(.+)/)
		{
			$file_details->{Season} = $1;
		}
		elsif($line =~ /Episode:(.+)/)
		{
			$file_details->{Episode} = $1;
		}
		elsif($line =~ /Subtitle:(.+)/)
		{
			$file_details->{Subtitle} = $1;
		}
		elsif($line =~ /Director:(.+)/)
		{
			$file_details->{Director} = $1;
		}
		elsif($line =~ /Plot:(.+)/)
		{
			$file_details->{Plot} = $1;
		}
		elsif($line =~ /Year:(.+)/)
		{
			$file_details->{Year} = $1;
		}
		elsif($line =~ /Cast:(.+)/)
		{
			$file_details->{Cast} = $1;
		}
		elsif($line =~ /Runtime:(.+)/)
		{
			$file_details->{Length} = $1;
		}
		elsif($line =~ /Episode Image:(.+)/)
		{
			$url = $1;
			if($url =~ /^http:/i )
			{
				$file_details->{ScreenshotUrl} = $url;
			}
		}
		elsif($line =~ /Screenshot:(.+)/)
		{
			$gotimages = 1;
			($file_details->{ScreenshotUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
		}
		elsif($line =~ /Coverart:(.+)/)
		{
			$gotimages = 1;
			($file_details->{PosterUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
		}
		elsif($line =~ /Fanart:(.+)/)
		{
			$gotimages = 1;
			($file_details->{FanartUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
		}
		elsif($line =~ /Banner:(.+)/)
		{
			$gotimages = 1;
			($file_details->{BannerUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
		}
		elsif($line =~ /Error contacting www\.thetvdb\.com/)
		{
			$broken = 1;
			print " Site appears to be down, disabling grabber\n";
		}
	}
	close(RESULT);
	if( $broken )
	{
		return;
	}
	if( $gotimages == 0 )
	{
		$ttvdb_pid = open(RESULT, "$ttvdb_script -PFBS \"$title\" $season $episode 2>&1 |");
		unless ($ttvdb_pid)
		{
			die "Can't run $ttvdb_script";
		}
		while ( $line = <RESULT>)
		{
#			print $line."\n";
			if($line =~ /^http:/i )
			{
				if( length $file_details->{ScreenshotUrl} == 0 )
				{
					$file_details->{ScreenshotUrl}=$line;
				}
			}
			elsif($line =~ /Coverart:(.+)/)
			{
				($file_details->{PosterUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
			}
			elsif($line =~ /Poster:(.+)/)
			{
				if( length $1 )
				{
					($file_details->{PosterUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
				}
				else
				{
					$1 = <RESULT>;
					($file_details->{PosterUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
				}
			}
			elsif($line =~ /Fanart:(.+)/)
			{
				if( length $1 )
				{
					($file_details->{FanartUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
				}
				else
				{
					$1 = <RESULT>;
					($file_details->{FanartUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
				}
			}
			elsif($line =~ /Banner:(.+)/)
			{
				($file_details->{BannerUrl}) = $1 =~ m/(http:\/\/.*?\.jpg)/;
			}
		}
	}
	close(RESULT);

}



1;
